#ifdef HAVE_CONFIG_H
  #include "config.h"
#endif

#include <time.h>
#include <gtest4c.h>
#include <gconn.h>
#include <gigione.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

#include <gigitest.h>

void nodes_callback(void* host,int nMsg,int lParam,int wParam)
{
#ifdef WINDOWS
  host;nMsg;lParam;wParam;
#endif
};

COMM_TEST (nodes, callback, 10)
{
  if (ctx->id == 0)
  {
    void *pCB = gigi_set_callback (ctx->context, nodes_callback, ctx->context);
    C_ASSERT_TRUE ( pCB );

    gigi_kill_callback (pCB);
  }
};

COMM_TEST (nodes, ntoa_aton, 2 )
{
  GIGI_ID myid, nextid, getid;
  GIGI_SOCKET fd;
  char s[64];

  myid = gigi_my_id (ctx->context);

  fd = create_channel (ctx, 0 , 1);
  C_ASSERT_TRUE (fd);

  getid  = myid;
  sendrecv (fd, GIGI_TYPE_ID, &getid, NULL, ctx->id, 0, 1);
  
  nextid = myid;
  sendrecv (fd, GIGI_TYPE_ID, &nextid, NULL, ctx->id, 1, 0);

  if (ctx->id == 1)
  {
    nextid = getid;
  }

  printf ("%d : myid = %s\n", ctx->id, gigi_ntoa (myid,s) );
  printf ("%d : peer = %s\n", ctx->id, gigi_ntoa (nextid, s) );

  getid = gigi_aton (ctx->context, s, 0);

  C_ASSERT_TRUE ( getid == nextid );

  printf ("%d : get  = %s\n", ctx->id, gigi_ntoa (getid, s) );

  gigi_closesocket (fd, 0);
};


COMM_TEST (types, fileType, 2)
{
  GIGI_STREAM fp = NULL;
  char fileName[GIGI_MAX_PATH];
  
  GenTempFileName (fileName, sizeof (fileName) );
  
  // Create File at Node 0;
  //
  if ( ctx->id == 0)
  {
    C_ASSERT_TRUE ( GenTempFile (fileName, 64 * 1024 ) >= 0 );
  }

  fp = gigi_fopen (fileName, ctx->id ? "w" : "r");
  C_ASSERT_TRUE (fp);
  
  gigi_fclose (fp);
}

const char* g_string = "types basicType test a b c d e f g f d s s s s "
    "sd f sd as as as d sd s    asd a  s as das as d asd asd as d asd "
    "s d s d f s d 2 1 3 4 1 2 3 s df sd23 4 df  sd f g sdf gsdf  sdfg sdfg sdfg sdf";

COMM_TEST (types, basicType, 2)
{
  char Buf[512];
  char** pStringList = ctx->id == 0 ? strings_split(g_string,' ') : NULL;
  GIGI_SOCKET fd;
  int nDataSize;

  fd = create_channel (ctx, 0, 1);

  C_ASSERT_TRUE (fd);

  if (ctx->id == 0)
  {
    C_ASSERT_TRUE ( pStringList );
    strcpy (Buf, g_string);
    nDataSize = (int) strlen (Buf) + 1;
  }
  else
  {
    nDataSize = sizeof (Buf);
  }

  sendrecv (fd, GIGI_TYPE_STRING, Buf, &nDataSize, ctx->id, 0, 1);
  C_ASSERT_TRUE ( strcmp (Buf, g_string) == 0 );

  if (ctx->id == 0 )
  {
    nDataSize = 0;
    Buf[0] = 0;
  }
  else
  {
    nDataSize = sizeof (Buf);
  }

  sendrecv (fd, GIGI_TYPE_STRING, Buf, &nDataSize, ctx->id, 0, 1);
  C_ASSERT_TRUE ( nDataSize == 0 );

  C_ASSERT_TRUE ( Buf[0] == 0 );

  if (ctx->id == 0)
  {
    nDataSize = strings_count ( (const char**) pStringList);
  }

  sendrecv (fd, GIGI_TYPE_ARGV, &pStringList, &nDataSize, ctx->id, 0, 1);

  C_ASSERT_TRUE (nDataSize == 65 );

  if (ctx->id != 0)
  {
    free (pStringList);
  }

  nDataSize = -1;
  sendrecv (fd, GIGI_TYPE_ARGV, &pStringList, &nDataSize, ctx->id, 0, 1);
  
  if (ctx->id != 0 )
  {
    C_ASSERT_TRUE ( nDataSize == 65);
  }

  free (pStringList);

  gigi_closesocket (fd, 1);
};

C_TEST (types, strings)
{
  char** stringList = strings_split (g_string,' ');
  char buf[64];
  char* pS1 = strings_join (buf, sizeof(buf), (const char **) stringList, -1, ' ','\"');
  char* pS2 = strings_join (NULL, 0, (const char **) stringList, -1, ' ','\"');

  C_ASSERT_TRUE ( stringList );
  C_ASSERT_TRUE ( pS1 && pS2 );
  
  fputs ("\n", stdout);
  fputs (pS1, stdout);

  fputs ("\n", stdout);
  fputs (pS2, stdout);
  
  free (stringList);
  free (pS2);
}

C_TEST_CASE_BEGIN (nodes_testCases)
  C_TEST_CASE (nodes, callback)
  C_TEST_CASE (nodes, ntoa_aton)
  C_TEST_CASE (types, basicType)
  C_TEST_CASE (types, fileType)
  C_TEST_CASE (types, strings)
C_TEST_CASE_END ()


